#!/bin/sh

if [ $# != 1 ] ; then
  echo "必须输入当前计算机的ip地址：例如：192.168.123.19"
  exit 1;
fi

kword=SNAPSHOT
function stop()
{

        PID=""
        query(){
                PID=`ps -ef |grep java|grep $kword|grep -v grep|awk '{print $2}'`
        }

        query
        if [ x"$PID" != x"" ]; then
                kill -TERM $PID
                while [ x"$PID" != x"" ]
                do
                    sleep 1
                    query
                done
        fi
}

echo "停止除seata外的所有微服务"
stop

echo "停止seata"
kword=seata
stop

#停止所有的容器
docker stop $(docker ps -a -q)
#删除所有容器
docker  rm $(docker ps -a -q)



cd /projects/woniuticket
rm -rf *

#下载代码
git clone https://gitee.com/tolryg0/woniuticket-parent.git

chmod -R 777 /projects/woniuticket/woniuticket-parent

cd /projects/woniuticket/woniuticket-parent


#启动mysql
docker run -d -p 3306:3306 --privileged=true  -v /docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
#启动rabbitmq
docker run -d --name rabbit  -p 15672:15672 -p 5672:5672  rabbitmq:management

#启动redis
docker run -p 6379:6379 --name redis -v /docker/redis/data:/data -d redis:6.2-alpine

#启动elasticsearch
docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.2.4

#复制ik分词器
docker cp /projects/woniuticket/woniuticket-parent/ik elasticsearch:/usr/share/elasticsearch/plugins

#重启elasticsearch容器
docker restart elasticsearch

#复制数据库脚本
docker cp /projects/woniuticket/woniuticket-parent/db/seata.sql mysql:/etc;
docker cp /projects/woniuticket/woniuticket-parent/db/woniu_cinema.sql mysql:/etc;
docker cp /projects/woniuticket/woniuticket-parent/db/woniu_cinema_coupon.sql mysql:/etc;
docker cp /projects/woniuticket/woniuticket-parent/db/woniu_cinema_order.sql mysql:/etc;
docker cp /projects/woniuticket/woniuticket-parent/db/woniu_cinema_report.sql mysql:/etc;
docker cp /projects/woniuticket/woniuticket-parent/db/woniu_cinema_sysadmin.sql mysql:/etc;
docker cp /projects/woniuticket/woniuticket-parent/db/woniu_cinema_user.sql mysql:/etc;
docker cp /projects/woniuticket/woniuticket-parent/db/woniu_generator.sql mysql:/etc;

#执行sql脚本
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/seata.sql';
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/woniu_cinema.sql';
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/woniu_cinema_coupon.sql';
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/woniu_cinema_order.sql';
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/woniu_cinema_report.sql';
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/woniu_cinema_sysadmin.sql';
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/woniu_cinema_user.sql';
docker exec -it mysql /bin/sh  -c 'mysql -uroot -p123456 -Dmysql</etc/woniu_generator.sql';

#编译
mvn clean package install

echo "启动所有的微服务，大概需要10--15分钟时间"

echo "启动eureka 需要90秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/eureka/target/eureka-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/eureka.log 2>&1 &

sleep 90

echo "启动seata 需要60秒"
nohup /projects/woniuticket/woniuticket-parent/seata/bin/seata-server.sh > /projects/woniuticket/woniuticket-parent/logs/seata.log 2>&1 &

sleep 60

echo "启动dashboard 需要60秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/dashboard/target/dashboard-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/dashboard.log 2>&1 &

sleep 60

echo "启动auth 需要60秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/auth/target/auth-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/auth.log 2>&1 &

sleep 60

echo "启动cinema-service 需要230秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/cinema/cinema-service/target/cinema-service-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/cinema-service.log 2>&1 &

sleep 230

echo "启动message 需要120秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/message/target/message-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/message.log 2>&1 &

sleep 120

echo "启动order-service 需要160秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/order/order-service/target/order-service-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/order-service.log 2>&1 &

sleep 160

echo "启动user-service 需要120秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/user/user-service/target/user-service-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/user-service.log 2>&1 &

sleep 120

echo "启动finance-service 需要200秒"
nohup java -jar  /projects/woniuticket/woniuticket-parent/finance/finance-service/target/finance-service-0.0.1-SNAPSHOT.jar --alipay.alipay_call_back_ip=$1 > /projects/woniuticket/woniuticket-parent/logs/finance-service.log 2>&1 &

sleep 200

echo "启动search-service 需要260秒"
nohup java -jar /projects/woniuticket/woniuticket-parent/search/search-service2/target/search-service2-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/search-service2.log 2>&1 &

sleep 260

echo "启动zuul"
nohup java -jar /projects/woniuticket/woniuticket-parent/zuul/target/zuul-0.0.1-SNAPSHOT.jar > /projects/woniuticket/woniuticket-parent/logs/zuul.log 2>&1 &


sleep 5

echo "微服务日志文件在/projects/woniuticket/woniuticket-parent/logs目录中"

echo "确保所有微服务都启动成功后开始访问系统"

